package com.zhipu.resetbalance.ui.home.feature.compute;

import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.zhipu.resetbalance.R;
import com.zhipu.resetbalance.databinding.ActivityComputeBinding;
import com.zhipu.resetbalance.mobile.dao.InspectorEntityDao;
import com.zhipu.resetbalance.mobile.event.InspectorEntity;
import com.zhipu.resetbalance.ui.BaseActivity;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;
import org.angmarch.views.SpinnerTextFormatter;

import java.util.List;

public class ComputeActivity extends BaseActivity<ActivityComputeBinding> {

    /**
     * VM
     */
    private ComputeViewModel mComputeViewModel;

    /**
     * 类型
     */
    private int breed = 0;

    /**
     * 检测人员
     */
    private InspectorEntity mInspectorEntity;

    @Override
    public void initParam() {
        super.initParam();
        breed = getIntent().getExtras().getInt("breed",0);


    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_compute;

    }



    @Override
    public void initViewModel() {
        super.initViewModel();
        //VM
        mComputeViewModel = new ViewModelProvider(getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(ComputeViewModel.class);
        binding.setViewModel(mComputeViewModel);

    }


    @Override
    public void initView() {
        super.initView();
        if(null != mComputeViewModel){
            mComputeViewModel.milliseconds();
            //类型
            mComputeViewModel.breedNumber.set(breed);
        }

        /**
         * 检测人员
         */
        List<InspectorEntity> mPersonnelList = InspectorEntityDao.getInstance().getAllList();
        if(null != mPersonnelList && mPersonnelList.size() > 0){
            SpinnerTextFormatter mSpinnerTextFormatter = new SpinnerTextFormatter<InspectorEntity>() {
                @Override
                public Spannable format(InspectorEntity item) {
                    mInspectorEntity = item;
                    return new SpannableString(mInspectorEntity.InspectorName);
                }
            };
            binding.NiceSpinner.setSpinnerTextFormatter(mSpinnerTextFormatter);
            binding.NiceSpinner.setSelectedTextFormatter(mSpinnerTextFormatter);
            binding.NiceSpinner.attachDataSource(mPersonnelList);
            binding.NiceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
                @Override
                public void onItemSelected(NiceSpinner parent, View view, int position, long id) {
                    mInspectorEntity = (InspectorEntity) binding.NiceSpinner.getSelectedItem();
                }
            });
        }
    }


    @Override
    public void initData() {
        super.initData();



    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();



    }

    @Override
    protected void onDestroy() {
        if(null != mComputeViewModel){
            mComputeViewModel.ReleaseTimer();
        }
        super.onDestroy();
    }
}
